<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>星火API调试中心</title>
    <script src="./js/jquery-3.5.1.min.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        body {
            background-color: #f5f5f5;
            font-family: Arial, sans-serif;
        }
        .chat-container {
            max-width: 800px;
            margin: 20px auto;
            background: white;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            display: flex;
            flex-direction: column;
            height: 90vh;
        }
        .chat-header {
            padding: 20px;
            border-bottom: 1px solid #eee;
            display: flex;
            align-items: center;
           
        }
        .chat-header img {
            width: 40px;
            height: 40px;
            margin-right: 10px;
        }
        .chat-header h1 {
            font-size: 20px;
            color: #333;
        }
        .chat-history {
            flex: 1;
            overflow-y: auto;
            padding: 20px;
        }
        .chat-input-container {
            padding: 20px;
            border-top: 1px solid #eee;
            display: flex;
            gap: 10px;
        }
        .chat-input {
            flex: 1;
            padding: 15px;
            border: 1px solid #ddd;
            border-radius: 5px;
            resize: none;
            height: 100px;
            font-size: 14px;
            background-color: #fff;
        }
        .send-btn {
            padding: 0 30px;
            background: #4e6ef2;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
        }
        .send-btn:hover {
            background: #4058d4;
        }
        .clear-btn {
            padding: 10px;
            color: #666;
            background: none;
            border: none;
            cursor: pointer;
            display: flex;
            align-items: center;
            gap: 5px;
        }
        .clear-btn:hover {
            color: #333;
        }
        .content-item {
            margin-bottom: 20px;
            padding: 15px 20px;
            background: #f7f8fa;
            border-radius: 10px;
            max-width: 80%;
        }

        .content-item p {
            margin: 0;
            line-height: 1.5;
            color: #333;
            font-size: 14px;
        }

        .content-item.user {
            margin-left: auto;
            background: #e8f0fe;
        }

        .content-item.ai {
            margin-right: auto;
            background: #f7f8fa;
        }

        .content-item.ai::before {
            content: '';
            display: inline-block;
            width: 24px;
            height: 24px;
            background: url('') no-repeat center/contain;
            vertical-align: middle;
            margin-right: 8px;
            margin-bottom: 4px;
        }
    </style>
</head>
<body>
    <div class="chat-container">
        <div class="chat-header">
            <h1>李艺的ai</h1>
            <button class="clear-btn" onclick="clearHistory()">
                <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                    <path d="M3 6h18M19 6v14a2 2 0 01-2 2H7a2 2 0 01-2-2V6m3 0V4a2 2 0 012-2h4a2 2 0 012 2v2"/>
                </svg>
                清除历史记录
            </button>
        </div>
        <div class="chat-history" id="chatHistory">
             <!-- <div class="content-item user">
                <p>你好，我是李艺的ai，我可以为您提供帮助。</p>
            </div>
            <div class="content-item ai">
                <p>您可以问我任何问题，我会尽力回答。</p>
            </div>  -->
        </div>
        <div class="chat-input-container">
            <textarea class="chat-input" placeholder="在此输入您想了解的内容，Shift+Enter换行" id="chatInput"></textarea>
            <button class="send-btn" onclick="sendMessage()">发送</button>
        </div>
    </div>

    <script>
        // 回车事件
        $('#chatInput').keyup(function(e){
            if (e.keyCode==13) {
                sendMessage()
            }
        })
        // 1.获取输入框的内容
        function sendMessage() {
            // 获取输入框的内容
            let val = $('.chat-input').val().trim()
            // 判断不能为空
            if (!val) return
            
            // 元素的插入
            $('#chatHistory').append(`
            <div class="content-item user">
                <p>${val}</p>
            </div>
            `)
                     // 元素的插入ai
                     $('#chatHistory').append(`
            <div class="content-item ai">
                <p></p>
            </div>
            `)
               // 滚动到底部
               $('#chatHistory').scrollTop($('#chatHistory')[0].scrollHeight)
            $('.chat-input').val('')
            getdata(val)

        }
        function printer(el,text,speed=50) {
            console.log(el);
            
            let i =0
            el.innerHTML=''
            function type(){
                if (i<text.length) {
                    el.innerHTML+=text.charAt(i)
                    i++
                    setTimeout(type,speed)
                       // 滚动到底部
                       $('#chatHistory').scrollTop($('#chatHistory')[0].scrollHeight)
                }
            }
            type()
            
        }

        function getdata(text) {
            $.ajax({
                url:'http://192.168.123.152:6215/api/xh',
                type:'post',
                data:{
                    text,
                    token:'ffSILLijQXXAbwusISnv:cipGrFiZiwzjmqlQnlVh'
                },
                success:function(res){
                    console.log(res.choices[0].message.content);
                    let val = res.choices[0].message.content
                    console.log($('.content-item.ai'));
                    
                    let el =$('.content-item.ai').last().find('p')[0]
                    // 设置文字
                    printer(el,val,50)
                    var msg = new SpeechSynthesisUtterance(val);
                        msg.rate = 5;
                        window.speechSynthesis.speak(msg);

                    // 滚动到底部
                    $('#chatHistory').scrollTop($('#chatHistory')[0].scrollHeight)
             
                    
                }
            })
            
        }
        
  
    </script>
</body>
</html>